package cn.damai.mine.StringBufferAndBuilder;

public class StringBuilderTest {
	
	/*如果线程安全则全部执行完成后字符串长度应该都是 10 * 1000 = 10000，测试结果中显示 StringBuffer 最终长度的确是 10000，
	StringBuilder 最大长度只有9956，说明 StringBuilder 非线程安全*/
	
	//可能出现的异常 内部数组的增长速度低于数据拼接的速度
	/*Exception in thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException
		at java.lang.System.arraycopy(Native Method)
		at java.lang.String.getChars(String.java:814)
		at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:422)
		at java.lang.StringBuilder.append(StringBuilder.java:136)
		at cn.damai.mine.StringBufferAndBuilder.StringBuilderTest$1.run(StringBuilderTest.java:18)
		at java.lang.Thread.run(Thread.java:745)*/
	
	private static StringBuilder param = new StringBuilder("");
	
	public static void main(String[] args) {
		
		Runnable run = new Runnable() {
			
			@Override
			public void run() {
					
				for (int i = 0; i < 1000; i++) {
					param.append("A");
				}
				System.out.println(Thread.currentThread().getName()+":"+param.length());
			}
		};
		
		for (int i = 0; i < 10; i++) {
			new Thread(run).start();
		}
	}

}
